home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5258 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: magnus.acs.ohio-state.edu!csn!ub!newserve!rebecca!rpi!not-for-mail
  2. From: Frederic LACHASSE <lachass@worldnet.fr>
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Re: Pointer Typecasts
  5. Date: 2 Feb 1996 15:05:37 -0000
  6. Organization: World-Net information exchange, Internet provider.
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: Dietmar.Kuehl@uni-konstanz.de
  9. Message-ID: <4et981$joh@netlab.cs.rpi.edu>
  10. References: <4ejnr9$cpf@netlab.cs.rpi.edu>
  11. Reply-To: lachass@worldnet.fr
  12. NNTP-Posting-Host: netlab.cs.rpi.edu
  13. X-Original-Date: Fri, 02 Feb 1996 13:39:13 +0000
  14.  
  15. reckdahl@leland.Stanford.EDU (Keith Reckdahl) wrote:
  16. > We've been having problems with the following pointer 
  17. > typecast: We have 4 classes A,B,C, and D
  18. >   B inherits from A
  19. >   C inherits from A
  20. >   D inherits from both B and C
  21. > Note: We've have also tried to inherit D from B and C 
  22. > using the virtual keyword so that only one instance 
  23. > of A is instantiated, but this does not seem to affect 
  24. > the problem.  We try to typecast a pointer from A to D 
  25. > meaning, in the code, we have
  26. >   A  *a1;
  27. >   D  *d1;
  28. >   d1 = (D *)a1;
  29. > And this does not work.  We get an illegal typecast 
  30. > compiler error.  We've tried this on a variety of 
  31. > compilers, including CODE WARRIOR, BORLAND, and MPW.
  32. > We thought that typecasting pointers is a simple 
  33. > matter because all pointers are simply addresses. 
  34. > What are we doing wrong?
  35.  
  36. Compilers are right: it's illegal. That's because they cannot know if 
  37. the pointer to A points to the B or C element of D:
  38.  
  39.          < A
  40.     /  B <
  41.     |    < B specific
  42.     |
  43.     |    < A
  44.   D <  C <
  45.     |    < C specific
  46.     |
  47.     \  D specific
  48.  
  49. But the following should work:
  50.  
  51.   d1 = (D *)(B *)a1;
  52.   d2 = (D *)(C *)a1;
  53.  
  54. with a different result of course.
  55.  
  56. If A is declared virtual in B and C, the problem is different: it is 
  57. illegal to cast a base virtual class to a derived class.
  58.  
  59. I don't have all my doc here, but may be dynamic_cast may work if you 
  60. have RTTI and virtual functions in A.
  61.  
  62.  Fridiric LACHASSE (ECP 86)
  63.  CompuServe: 100530,2005
  64.  Internet: lachass@worldnet.fr
  65.  
  66.  
  67.  
  68.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  69.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  70.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  71.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  72.